package com.lily.aidouzixun.ui;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lily.aidouzixun.R;
import com.lily.aidouzixun.activity.ArticleDetailActivity;
import com.lily.aidouzixun.bean.ArticleInfo;
import com.lily.aidouzixun.util.MyConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * 轮播控件
 */
public class CarouselView extends FrameLayout implements ViewPager.OnPageChangeListener {
    private static final String TAG = "CarouselView";
    /**
     * 上下文
     */
    private Context context;
    /**
     * 图片控件列表
     */
    private List<ImageView> imageList;
    /**
     * ViewPage
     */
    private ViewPager viewPager;
    /**
     * 文章标题
     */
    private TextView tvTitle;
    /**
     * 点控件列表
     */
    private List<ImageView> dotViewList;
    /**
     * 图片的url
     */
    private List<String> urls;
    /**
     * 轮播信息
     */
    private SparseArray<ArticleInfo> carouselList;

    /**
     * 页数
     */
    private int pagerSize = 0;
    /**
     * 构造方法
     * @param context
     */
    public CarouselView(Context context) {
        super(context);
    }
    public CarouselView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CarouselView(Context context, AttributeSet attrs,int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 初始化信息
     * @param
     */
    public void setData(Context context ,SparseArray<ArticleInfo> carouselList) {
        Log.v(TAG, "setData");
        if(carouselList == null || carouselList.size() <= 0){
            return;
        }
        this.carouselList = carouselList;
        this.context = context;
        pagerSize = carouselList.size();

        View view = LayoutInflater.from(context).inflate(R.layout.carouselview, this, true);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        tvTitle = (TextView) this.findViewById(R.id.tv_title);
        RelativeLayout rlLongDistanceBottom = (RelativeLayout) this.findViewById(R.id.rl_longdistance_bottom);
        rlLongDistanceBottom.setBackgroundColor(R.color.a5_black);
        //初始化一个
        tvTitle.setText(carouselList.get(0).getTitle());
        LinearLayout dotLinearLayout = (LinearLayout) view.findViewById(R.id.dotLayout);
        //先清除所有点，防止刷新时累加
        dotLinearLayout.removeAllViews();
        //初始化viewpager上显示的ImageView
        imageList = new ArrayList<ImageView>();
        dotViewList = new ArrayList<ImageView>();

        for(int i = 0;i < pagerSize;i++){
            View imageviewView = LayoutInflater.from(context).inflate(R.layout.imageview, null);
            ImageView imageView = (ImageView) imageviewView.findViewById(R.id.imageview);
            imageList.add(imageView);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            params.leftMargin = 4;
            params.rightMargin = 4;
            dotViewList.add(imageView);
            dotLinearLayout.addView(imageView, params);


           /* PointView dot = new PointView(context);
            if(i == 0 ){
                dot.setColorAndSize(Color.BLUE, 12);
            }else{
                dot.setColorAndSize(Color.GRAY, 12);
            }
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(50, 70);
            dotViewList.add(dot);*/
            //todo
            // dotLinearLayout.addView(dot, lp);
        }
        Log.v(TAG, "imageList.size="+imageList.size());
        viewPager.setAdapter(new MyViewPagerAdapter(imageList));
        viewPager.setOnPageChangeListener(this);
    }

    /**
     * 自定义的PagerAdapter
     */
    class MyViewPagerAdapter extends PagerAdapter {
        List<ImageView> imageList;

        public MyViewPagerAdapter(List<ImageView> imageList){
            this.imageList = imageList;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return imageList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            ((ViewPager)container).removeView(imageList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            ImageView imageView = imageList.get(position);
            //点击事件
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    ArticleInfo longDistanceInfo = carouselList.get(position);
                    String ldid = longDistanceInfo.getLdid();
                    String param = "ldid=" + ldid;
                    ArticleDetailActivity.startWebViewActivity(context, longDistanceInfo, MyConstant.BASE_URL + "longdistance/itemdetail?page=1&" + param, "文章详情", "首页", true);
                }
            });
            container.addView(imageView);
            Glide.with(context)
                    .load(carouselList.get(position).getImageUrl())
                    .into(imageView);
            //Log.v(TAG, "position-->"+position);
            return imageView;

        }

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
    }
    /**
     * 页面改变的监听
     */
    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub
        Log.v(TAG, "onPageSelected--"+arg0);
        tvTitle.setText(carouselList.get(arg0).getTitle());
        for(int i = 0;i<pagerSize;i++){
            /*if(i == arg0){
                PointView dot= dotViewList.get(i);
                dot.setColor(Color.BLUE);
                dot.invalidate();
            }else{
                PointView dot= dotViewList.get(i);
                dot.setColor(Color.GRAY);
                dot.invalidate();
            }*/
        }
    }
}
